#include "light_cam.h"

eTB_LightCam::eTB_LightCam(e3dVector3 inPosition3, 
           float inFov, 
           float inAspect, 
           float inNearZ, 
           float inFarZ)
{
  m_Position3 = inPosition3;
  m_fFOV = inFov;
  m_fAspectRatio = inAspect;
  m_fNearZ = inNearZ;
  m_fFarZ = inFarZ;
  m_Up = e3dVector3 (0.0f, 1.0f, 0.0f);
  m_Look3 = e3dVector3 (0.0f, 0.0f, 1.0f);
  m_Right = e3dVector3 (1.0f, 0.0f, 0.0f);
  m_CamInitialY = m_Position3.y;

  updateViewProjMat();
}

e3dVector3& eTB_LightCam::getPosition()
{
  return m_Position3;
}

void eTB_LightCam::setPosition(e3dVector3 inPosition)
{
  m_Position3 = inPosition;
//  m_Position4.x = m_Position3.x;
//  m_Position4.y = m_Position3.y;
//  m_Position4.z = m_Position3.z;
//  m_Position4.w = 1.0f;
}

void eTB_LightCam::updateViewProjMat()
{
  updateViewMat(); 
  setProjMat(); 

  m_ViewProjMat = m_ViewMat * m_ProjMat;
//  D3DXMatrixMultiply(&m_ViewProjMat, &m_ViewMat, &m_ProjMat);
}

//D3DXVECTOR4* eTB_LightCam::getPosition4()
//{
//  return &m_Position4;
//}

e3dMatrix4x4& eTB_LightCam::getViewMat()
{
  return m_ViewMat;
}

e3dMatrix4x4& eTB_LightCam::getProjMat()
{
  return m_ProjMat;
}

e3dMatrix4x4& eTB_LightCam::getViewProjMat()
{
  return m_ViewProjMat;
}

e3dVector3& eTB_LightCam::getLook()
{
  return m_Look3;
}

//D3DXVECTOR4* eTB_LightCam::getLook4()
//{
//  return &m_Look4;
//}

e3dVector3& eTB_LightCam::getRight()
{
  return m_Right;
}

e3dVector3& eTB_LightCam::getUp()
{
  return m_Up;
}

void eTB_LightCam::setLook(e3dVector3 inLook)
{
  m_Look3 = *inLook;
//  m_Look4.x = m_Look3.x;
//  m_Look4.y = m_Look3.y;
//  m_Look4.z = m_Look3.z;
//  m_Look4.w = 1.0f;
}

void eTB_LightCam::setUp(e3dVector3 inUp)
{
  m_Up = inUp;
}

void eTB_LightCam::setRight(e3dVector3 inRight)
{
  m_Right = inRight;
}



void eTB_LightCam::updateViewMat()
{
  m_Look3.Normalize ();
  m_Up = m_Look3 % m_Right;
  m_Up.Normalize ();
  m_Right = m_Up % m_Look3;
  m_Right.Normalize ();

  m_ViewMat.m11 = m_Right.x; m_ViewMat.m12 = m_Up.x; m_ViewMat.m13 = m_Look3.x; m_ViewMat.m14 = 0.0f;
  m_ViewMat.m21 = m_Right.y; m_ViewMat.m22 = m_Up.y; m_ViewMat.m23 = m_Look3.y; m_ViewMat.m24 = 0.0f;
  m_ViewMat.m31 = m_Right.z; m_ViewMat.m32 = m_Up.z; m_ViewMat.m33 = m_Look3.z; m_ViewMat.m34 = 0.0f;
  m_ViewMat.m41 = -(m_Right * m_Position3);       
  m_ViewMat.m42 = -(m_Up * m_Position3); 
  m_ViewMat.m43 = -(m_Look3 * m_Position3);      
  m_ViewMat.m44 = 1.0f;
}

#include "../../Epsilon/Epsilon3D/OpenGL.h"

void eTB_LightCam::setProjMat()
{
  glMatrixMode (GL_PROJECTION);
  glPushMatrix ();
  glLoadIdentity ();
  gluPerspective (m_fFOV, m_fAspectRatio, m_fNearZ, m_fFarZ);
  glGetFloatv (GL_PROJECTION_MATRIX, m_ProjMat.m);
  glPopMatrix ();
  //D3DXMatrixPerspectiveFovLH( &m_ProjMat, m_fFOV, m_fAspectRatio, m_fNearZ, m_fFarZ );
}



void eTB_LightCam::updateCamera()
{
  updateViewMat();
  m_ViewProjMat = m_ViewMat * m_ProjMat;
}
